package com.snail.consumer;

import com.snail.constants.RabbitMqConstants;
import com.snail.model.pojo.BizMediaDoc;
import com.snail.model.pojo.BizMediaFile;
import com.snail.service.BizMediaDocService;
import com.snail.service.BizMediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @author Monica
 * @description
 * @date 2025/7/19 14:06
 */
@Component
@Slf4j
public class VideoConsumer {
    @Value("${minio.endpoint}")
    private String minioEndpoint;
    @Value("${minio.bucket.video}")
    private String videoBucket;

    @Autowired
    private BizMediaFileService bizMediaFileService;
    @Autowired
    private BizMediaDocService bizMediaDocService;

    public static final String TEMP_DIR = System.getProperty("user.dir") + "/temp";

    @RabbitListener(queues = RabbitMqConstants.VIDEO_DIRECT_QUEUE)
    public void videoConsumer(BizMediaFile message) throws IOException {
       try {
           Integer videoId = message.getId();
           String videoUrl = minioEndpoint + "/" + videoBucket + "/" + message.getFilePath();
           log.info("视频开始下载:{}",videoUrl);
           Path path = bizMediaFileService.downloadVideoFile(videoUrl);
           log.info("视频下载完成:{}",videoUrl);
           File videoFile = path.toFile();
           File audioFile = new File(Paths.get(TEMP_DIR).toAbsolutePath() + "/audio_" + UUID.randomUUID() + ".wav");

           bizMediaFileService.convertVideoToAudio(videoFile, audioFile);

           // 定义输出音频文件路径
           log.info("音频开始下载" + audioFile.getPath());
           bizMediaFileService.convertVideoToAudio(videoFile, audioFile);
           log.info("音频下载完成" + audioFile.getPath());
           // 2. 音频转文字
           String subtitleText = bizMediaFileService.transcribeAudioToString(audioFile);
           System.out.println("-------------" + subtitleText);
           // 3. 创建字幕记录
           BizMediaDoc bizMediaDoc = new BizMediaDoc();
           bizMediaDoc.setContent(subtitleText);
           bizMediaDoc.setVideoId(videoId);
           // 手动赋值更新文档记录
           bizMediaDocService.saveWithoutAutoFill(bizMediaDoc);
           // 更新视频记录
           message.setSubtitleId(bizMediaDoc.getId());
           bizMediaFileService.updateByIdWithoutAutoFill(message);
       } catch (Exception e) {
           throw new RuntimeException(e);
       }  finally {
           // 删除临时文件
           Files.list(Paths.get(TEMP_DIR)).filter(Files::isRegularFile).forEach(path -> {
               try {
                   Files.delete(path);
               } catch (IOException e) {
                   System.err.println("删除失败: " + path);
               }
           });
       }

    }
}
